// pages/results/results.js
const db= wx.cloud.database().collection('user')
Page({
    data:{
       randomList:'',
       oknum:'',
       total:'',
       min:'',
       sec:'',
       score:0,
       rate:0,
       allerr:0,
       err:0
    },
    onShow(){
      this.addCollection()
    },
    onLoad(options){
      let user =  wx.getStorageSync('userInfo')
      if(user == null){
          this.setData({
              loginStatu:false
          })
      }else{
          this.setData({
            loginStatu:true
          })
      }
      //错题数
      this.setData({
        err:options.total - options.oknum
      })
      //计算正确率，获得整数
        let ran =(options.oknum/options.total)*100
        let ranum = Math.round(ran)
        // console.log(ranum)
        let scores = Number(options.score) 
        let totals = Number(options.total)
        this.setData({
            total:totals,
            oknum:options.oknum,
            min:options.min,
            sec:options.sec,
            score:scores,
            rate:ranum
        })
        // console.log(this.data.score,'分')

        this.addScoreToUser()

        //生成随机题库列表3个
        wx.cloud.database().collection('questbank')
        .aggregate()
        .sample({
          size:3
        })
        .end().then(  res => {
            this.setData({
                randomList:res.list
            }) 
            // console.log(this.data.randomList)
        })
 
    },
   async addScoreToUser(){

        // 获取原来分数   
        let user =  wx.getStorageSync('userInfo')
        // console.log(user._openid)、
        if(user==null){return}
        let {data} = await db.where({
            _openid:user._openid
        }).get()
        // console.log('1',data[0].score)
        let score,total,allerr;
        score = this.data.score+data[0].score
        total = this.data.total+data[0].total
        allerr = this.data.err+data[0].allerr

        //计算总正确率
        let rate =   Math.round(((total-allerr)/total)*100)
      
    
        //添加分数到数据库
        await db.where({
            _openid:user._openid
        }).update({
           data:{
             score,
             total,
             allerr,
             rate
           }
        })

    },
    goHome(){
        wx.switchTab({
          url: '/pages/index/index',
        })
    },
    gologin(){
        wx.switchTab({
            url: '/pages/mycenter/mecenter',
          })
    },
     //跳转错题本
      errTi(){
        wx.navigateTo({
          url: '/pages/wrong/wrong',
        })
     },
      //跳转排行榜
      matchList(){
        wx.navigateTo({
          url: '/pages/anlist/anlist',
        })
      },
      //跳转到收藏
      collection(){
        wx.navigateTo({
          url: '/pages/starme/starme',
        })
      },
      //继续答题
      daTi(){
        // //函数查找
            wx.cloud.database().collection('questbank')
            .get()
            .then(res => {
                 console.log("success2",res)
                 this.setData({
                   questid:res.data
                  })

                  let numid = Math.random();           
                  numid = numid*this.data.questid.length-1;  //0——10的随机数
                  numid = Math.ceil(numid); 
                  console.log(numid)
                 wx.redirectTo({
                     url: '../questdetail/questdetail?id='+this.data.questid[numid]._id,
            })
        }).catch(err => {
            console.log("err",err)
        })
       
      },
     //开始做题
    startQuestion(e){
      wx.navigateTo({
        url: '/pages/questdetail/questdetail?id='+e.currentTarget.dataset.id,
      })
  
      },
      allRightRate(){
        
      },
            //把收藏的的添加
       async addCollection(){
          let coll = wx.getStorageSync('coll')
       if(coll==null){
          return
       }

       let db = wx.cloud.database().collection('collection')
       let user = wx.getStorageSync('userInfo') 
  
       if(user==null){return}
       //查询此用户的错题表
       let find = await db.where({
           _openid:user._openid
       }).get()
       console.log(find)
     
        //判断是否相同的题

       for(let i=0;i<coll.length;i++){
         //  console.log("01")
         for(let j=0;j<find.data.length;j++){
           // console.log("02")
           if(coll[i]==null){break}
           if(find.data[j].questionID==coll[i]._id){
              // console.log("03")
                return
           }
         }

          if(coll[i] != null)
          {
            let questionall= coll[i]
            let questionID = coll[i]._id
          
              //添加操作
                 await db.add({
                   data:{
                       questionID,
                       questionall
                   }
               })
             }
        }
      },
   

 
})